package ua.kiev.tosik.price.database;

import org.apache.commons.lang.StringEscapeUtils;

import java.text.StringCharacterIterator;
import java.util.Date;

public class DbUtils {

	public static int countPages(int total, int resPerPage) {
		if ( total%resPerPage > 0 ) {
			return ((int)(total/resPerPage))+1;
		} else {
			return ((int)(total/resPerPage));
		}
	}

	public static boolean stringEquals(String one, String two) {
		String s1 = (one == null)?"":one;
		String s2 = (two == null)?"":two;
		return (s2.equals(s1));
	}

	public static String encodeForSql(String string) {
		return StringEscapeUtils.escapeSql(string);
	}

	public static boolean dateEquals(java.util.Date dateInDb, java.util.Date dateOfCurrentRecord) {
		/**
		 * lastUpdate in Db is null. You are the fist editor of this record
		 */
		if ( dateInDb == null ) {
			return true;
		}
		/**
		 * When open record before then other user edit it. Then your lastUpdate = null, lastUpdate in Db is not null
		 */
		if ( dateOfCurrentRecord == null ) {
			return false;
		}
		return dateInDb.equals(dateOfCurrentRecord);
	}

	public static String addSlashes(String text) {
		final StringBuffer sb = new StringBuffer(text.length() * 2);
		final StringCharacterIterator iterator = new StringCharacterIterator(text);

		char character = iterator.current();

		while (character != StringCharacterIterator.DONE) {
			if (character == '"') {
				sb.append("\\\"");
			} else {
				if (character == '\'') {
					sb.append("\\\'");
				} else {
					if (character == '\\') {
						sb.append("\\\\");
					} else {
						if (character == '\n') {
							sb.append("\\n");
						} else {
							if (character == '{') {
								sb.append("\\{");
							} else {
								if (character == '}') {
									sb.append("\\}");
								} else {
									sb.append(character);
								}
							}
						}
					}
				}
			}

			character = iterator.next();
		}

		return sb.toString();
	}

	public static String getTimeDiff(Date startTime, Date endTime) {
		long time = (endTime.getTime()-startTime.getTime()) / 1000;
		String seconds = Integer.toString((int) (time % 60));
		String minutes = Integer.toString((int) ((time % 3600) / 60));
		String hours = Integer.toString((int) (time / 3600));
		for (int i = 0; i < 2; i++) {
			if (seconds.length() < 2) {
				seconds = "0" + seconds;
			}
			if (minutes.length() < 2) {
				minutes = "0" + minutes;
			}
			if (hours.length() < 2) {
				hours = "0" + hours;
			}
		}
		// logger.debug("Application working time: " + hours + ":" + minutes + ":" + seconds);
		return hours + ":" + minutes + ":" + seconds;
	}

	public static String getTimeDiff(long timeMillisStart, long timeMillisEnd) {
		long time = (timeMillisStart-timeMillisEnd) / 1000;
		String seconds = Integer.toString((int) (time % 60));
		String minutes = Integer.toString((int) ((time % 3600) / 60));
		String hours = Integer.toString((int) (time / 3600));
		for (int i = 0; i < 2; i++) {
			if (seconds.length() < 2) {
				seconds = "0" + seconds;
			}
			if (minutes.length() < 2) {
				minutes = "0" + minutes;
			}
			if (hours.length() < 2) {
				hours = "0" + hours;
			}
		}
		return hours + ":" + minutes + ":" + seconds;
	}

}
